package cn.stronglink.asset.terminal.bjc.scrap.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.model.ItAssetExtPosition;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetExtPositionMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.assetmanage.scrap.mapper.ScrapMapper;
import cn.stronglink.asset.terminal.bjc.scrap.mapper.ScrapApiMapper;
import cn.stronglink.asset.terminal.bjc.scrap.vo.ScrapApiVo;
import cn.stronglink.asset.terminal.bjc.scrap.vo.ScrapDetailApiVo;

@Service
@Transactional(rollbackFor=Exception.class) 
public class ScrapApiService {

	@Autowired 
	private ScrapApiMapper scrapApiMapper;
	@Autowired
	private ScrapMapper scrapMapper;
	@Autowired
	private ItassetMapper itassetMapper;
	@Autowired
	private ItassetExtPositionMapper assetPositionMapper;
	public List<ScrapApiVo> queryScrapList(Map<String, Object> params) {
		List<ScrapApiVo> list = scrapApiMapper.queryScrapList(params);
		if(list!=null&&list.size()>0) {
			for(ScrapApiVo scrapApiVo:list) {
				List<ScrapDetailApiVo> detailList = scrapApiMapper.queryScrapDetailList(scrapApiVo.getId());
				scrapApiVo.setScrapDetailList(detailList);
			}
		}
		return list;
	}

	public void doScrap(ScrapApiVo scrap) {
		if(scrap.getId()!=null) {
			scrap.setStatus(3);
			scrap.setUpdateBy(scrap.getOperatorBy());
			scrap.setUpdateTime(new Date());
			scrapMapper.updateScrap(scrap);
		}else {
			scrap.setId(IdWorker.getId());
			scrap.setRoomId(scrap.getRoomId());
			scrap.setExecuteTime(new Date());
			Long uid = scrap.getCreateBy();
			scrap.setOperatorBy(uid);
			scrap.setStatus(3);
			scrap.setRemark("手持机操作");
			scrap.setCreateTime(new Date());
			scrapMapper.insertScrap(scrap);
		}
		if(scrap.getScrapDetailList()!=null&&scrap.getScrapDetailList().size()>0) {
			for(ScrapDetailApiVo scrapDetail:scrap.getScrapDetailList()){
				if(scrapDetail.getId()!=null) {
					scrapDetail.setStatus(1);
					scrapDetail.setUpdateBy(scrap.getOperatorBy());
					scrapDetail.setUpdateTime(new Date());
					scrapMapper.updateScrapDetail(scrapDetail);
				}else {
					scrapDetail.setId(IdWorker.getId());
					scrapDetail.setScrapId(scrap.getId());
					scrapDetail.setAssetId(Long.parseLong(scrapDetail.getAssetId_()));
					scrapDetail.setStatus(1);
					scrapDetail.setCreateBy(scrap.getCreateBy());
					scrapDetail.setCreateTime(new Date());
					scrapMapper.insertScrapDetail(scrapDetail);
				}
				
				ItAsset asset = new ItAsset();
				asset.setId(scrapDetail.getAssetId());
				asset.setStatus(3);//报废
//				asset.setRecoveryTag(scrapDetail.getRecoveryTag());   // 回收标记
				itassetMapper.updateItAsset(asset);
				ItAssetExtPosition assetPosition =new ItAssetExtPosition();
				assetPosition = new ItAssetExtPosition();
				assetPosition.setAssetId(Long.parseLong(scrapDetail.getAssetId_()));
				assetPosition.setUpdateBy(scrapDetail.getCreateBy());
				assetPosition.setUpdateTime(new Date());
				assetPositionMapper.updateItAssetExtPositionByAssetId(assetPosition);//更新资产位置信息
			}
		}
	}
	
}
